package com.work.game.ad;


import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.work.game.utils.UIUtils;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;

public class MediationBannerAd {
    private static final String TAG = "MediationBannerAd";
    private Activity activity;
    private String codeId;
    private ViewGroup viewGroup;
    private Integer height;
    private TTNativeExpressAd ttNativeExpressAd;

    public MediationBannerAd() {
    }

    public static volatile MediationBannerAd instance;

    public static synchronized MediationBannerAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (MediationBannerAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new MediationBannerAd();
                }
            }
        }
        return instance;
    }

    public void init(Activity activity, String codeId, Integer height) {
        this.activity = activity;
        this.codeId = codeId;
        this.height = height;
    }


    public void loadCacheBannerAd() {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        adNativeLoader.loadBannerExpressAd(buildBannerAdslot(codeId, height), new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                //广告加载失败
                Log.d(TAG, "onError: " + errorMsg);
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                //广告加载成功
                if (list != null && list.size() > 0) {
                    getInstance().ttNativeExpressAd = list.get(0);
                }
            }
        });
    }

    //展示Banner广告
    public void showCacheBannerView() {
        if (activity == null || ttNativeExpressAd == null) {
            loadBannerAd();
            return;
        }

        ttNativeExpressAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int i) {
                //广告点击
            }

            @Override
            public void onAdShow(View view, int i) {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = ttNativeExpressAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                }
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
                //广告渲染失败
            }

            @Override
            public void onRenderSuccess(View view, float v, float v1) {
                //广告渲染成功
            }
        });

        ttNativeExpressAd.setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onShow() {

            }

            @Override
            public void onSelected(int position, String value, boolean enforce) {
                //用户点击了dislike按钮
                viewGroup.removeAllViews();
            }

            @Override
            public void onCancel() {

            }
        });
        if (ttNativeExpressAd.getMediationManager().isReady()) {
            View bannerView = ttNativeExpressAd.getExpressAdView(); //获取Banner View
            if (bannerView != null) {
                viewGroup.removeAllViews();
                viewGroup.addView(bannerView);
            }
            loadCacheBannerAd();
        } else {
            loadBannerAd();
        }
    }

    private void loadBannerAd() {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        adNativeLoader.loadBannerExpressAd(buildBannerAdslot(codeId, height), new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                //广告加载失败
                Log.d(TAG, "onError: " + errorMsg);
            }
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                //广告加载成功
                if (list != null && list.size() > 0) {
                    ttNativeExpressAd = list.get(0);
                    showBannerView(activity, viewGroup); //注 ：bannerContainer为展示Banner广告的容器
                }
            }
        });
    }

    //展示Banner广告
    private void showBannerView(Activity act, ViewGroup container) {
        if (ttNativeExpressAd == null || container == null) {
            return;
        }

        ttNativeExpressAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int i) {
                //广告点击
            }

            @Override
            public void onAdShow(View view, int i) {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = ttNativeExpressAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                }
            }

            @Override
            public void onRenderFail(View view, String s, int i) {
                //广告渲染失败
            }

            @Override
            public void onRenderSuccess(View view, float v, float v1) {
                //广告渲染成功
            }
        });

        ttNativeExpressAd.setDislikeCallback(act, new TTAdDislike.DislikeInteractionCallback() {
            @Override
            public void onShow() {

            }

            @Override
            public void onSelected(int position, String value, boolean enforce) {
                //用户点击了dislike按钮
                container.removeAllViews();
            }

            @Override
            public void onCancel() {

            }
        });
        View bannerView = ttNativeExpressAd.getExpressAdView(); //获取Banner View
        if (bannerView != null) {
            container.removeAllViews();
            container.addView(bannerView);
        }
        loadCacheBannerAd();
    }


    //Banner 广告位 start
    private AdSlot buildBannerAdslot(String codeId, Integer height) {
        int width = UIUtils.getScreenWidthInPx(activity);
        return new AdSlot.Builder().setCodeId(codeId)  //广告位ID
                .setImageAcceptedSize(width, height)  //设置广告宽高 单位px
                .build();
    }

    public ViewGroup getViewGroup() {
        return viewGroup;
    }

    public void setViewGroup(ViewGroup viewGroup) {
        this.viewGroup = viewGroup;
    }
}
